package com.createidea.cms.dao.product;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.createidea.cms.dao.BaseDaoImpl;
import com.createidea.cms.to.PictureTo;
import com.createidea.cms.to.ProductTo; 
import com.createidea.cms.utils.Pager;

public class ProductDaoImpl extends BaseDaoImpl implements ProductDao {

	@Override
	public List<ProductTo> getProductsByStatus(int i) {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ProductTo.class);
		detachedCriteria.add(Restrictions.eq("status", i ));
		return this.getHibernateTemplate().findByCriteria(detachedCriteria);
	}

	@Override
	public List<ProductTo> getAllEnableProductsByPager(Pager pager, int i) {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ProductTo.class);
		detachedCriteria.add(Restrictions.eq("status", i ));
		detachedCriteria.addOrder(Order.desc("updatedTime"));
		return this.findResultForPager(detachedCriteria, pager);
	}

	@Override
	public void saveProcut(ProductTo product) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(product);
	}

	@Override
	public ProductTo getProcut(String productId) {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ProductTo.class);
		detachedCriteria.add(Restrictions.eq("id", productId ));		
		List <ProductTo> list= this.getHibernateTemplate().findByCriteria(detachedCriteria);
		if(list!=null&&list.size()>0)
			return list.get(0);
		else
			return null;
	}



	@Override
	public void savePicture(PictureTo picture) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(picture);
	}

	@Override
	public List<ProductTo> getProductsForType(int i, String producttypeId) {
		// TODO Auto-generated method stub
		
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ProductTo.class);		 	
		detachedCriteria.createCriteria("type").add(Restrictions.eq("id", producttypeId));
		detachedCriteria.add(Restrictions.eq("status", i ));
		detachedCriteria.addOrder(Order.desc("updatedTime"));		
		return	this.getHibernateTemplate().findByCriteria(detachedCriteria);
	}

	@Override
	public List<ProductTo> getProductsForTypeInPager(int i,String producttypeId,Pager pager) {
		// TODO Auto-generated method stub
	
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ProductTo.class);		 
		detachedCriteria.createCriteria("type").add(Restrictions.eq("id", producttypeId));
		detachedCriteria.add(Restrictions.eq("status", i ));
		detachedCriteria.addOrder(Order.desc("updatedTime"));		
		return	this.findResultForPager(detachedCriteria, pager);
	}
	
	@Override
	public List<ProductTo> getProductsByLan(int i, String lan, int status) {
		// TODO Auto-generated method stub
		String[] lang1={this.CH,this.ANYLAN};
		String[] lang2={this.EN,this.ANYLAN};
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ProductTo.class);		 
		if("zh".equals(lan))			
			detachedCriteria.createCriteria("language").add(Restrictions.in("chvalue", lang1));
		if("en".equals(lan))			
			detachedCriteria.createCriteria("language").add(Restrictions.in("chvalue", lang2));
		detachedCriteria.addOrder(Order.desc("updatedTime"));
		detachedCriteria.add(Restrictions.eq("status", status ));
		if(i==0)
			return	this.getHibernateTemplate().findByCriteria(detachedCriteria);
		else
			return this.getHibernateTemplate().findByCriteria(detachedCriteria, 0,i);
	}
	
	@Override
	public List<ProductTo> getProductsByLanInPager(int i, String lan, Pager pager) {
		// TODO Auto-generated method stub
		String[] lang1={this.CH,this.ANYLAN};
		String[] lang2={this.EN,this.ANYLAN};
		DetachedCriteria detachedCriteria=DetachedCriteria.forClass(ProductTo.class);		 
		if("zh".equals(lan))			
			detachedCriteria.createCriteria("language").add(Restrictions.in("chvalue", lang1));
		if("en".equals(lan))			
			detachedCriteria.createCriteria("language").add(Restrictions.in("chvalue", lang2));
		
		detachedCriteria.add(Restrictions.eq("status", i ));
		detachedCriteria.addOrder(Order.desc("updatedTime"));		
		return	this.findResultForPager(detachedCriteria, pager);
	}

}
